﻿using KinonekoSoftware.ArkSpark.Core;
using KinonekoSoftware.CodariaDB.Documents.DataParts.Modules;
using KinonekoSoftware.CodariaDB.Utils;
using KinonekoSoftware.Extensions.Strings.Patterns;


namespace KinonekoSoftware.Studio.Models.Modules
{
    public sealed class ParagraphBlockPresenter : GroupPresenter<Block_Paragraph>, IBlockPresenter, IHighlightDataContext
    {
        private int _isHighlightInformationChanged;

        public ParagraphBlockPresenter(Block_Paragraph target, IModuleMessageBus queue)
        {
            Highlights = new ObservableCollection<IHighlightToken>();
            Queue      = queue ?? throw new ArgumentNullException(nameof(queue));
            Initialize(target);
        }


        protected override Block_Paragraph CreateDummyInstance() => Block_Paragraph.CreateInstance();

        protected override void OnInitialize(Block_Paragraph target)
        {
            RaiseUpdated(nameof(Fallback));
            RaiseUpdated(nameof(Value));
        }

        /// <summary>
        /// 获得当前内容块的标题。
        /// </summary>
        /// <returns>返回当前内容块的标题。</returns>
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_Block_Paragraph : Title;
        public override bool IsMultipleVariants() => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;
        public override Variant GetVariant(string documentID)
        {
            return new StringVariant
            {
                Id       = documentID,
                Metadata = Metadata,
                Value    = Value,
                Type     = VariantType.String,
            };
        }
        
        public override void DataSync(Variant variant)
        {
            if (!string.IsNullOrEmpty(variant.Metadata) &&
                Metadata == variant.Metadata            &&
                variant is StringVariant sv)
            {
                Value = sv.Value;
            }
        }

        public ModuleBlock GetBlock() => AssociatedObject;

        protected override Task<bool> OnEditItem() => null;

        protected override void OnIndexChanged(int oldIndex, int newIndex)
        {
            AssociatedObject.Index = newIndex;
        }

        public IList<IHighlightToken> GetHighlightGlossaries() => Highlights;

        public void UpdateHighlightsInformation()
        {
            IsHighlightInformationChanged++;
        }

        public string Fallback
        {
            get => AssociatedObject?.Fallback;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Fallback = value;
                RaiseUpdated();
            }
        }

        public string Metadata
        {
            get => AssociatedObject?.Metadata;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                RaiseMetadataChanged(AssociatedObject.Metadata, value);
                AssociatedObject.Metadata = value;
                RaiseUpdated();
            }
        }

        public string Value
        {
            get => AssociatedObject?.Value;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                if (value == AssociatedObject.Value)
                {
                    return;
                }

                AssociatedObject.Value = value;
                RaiseUpdated();
                RaisePropertyUpdated(Metadata);
            }
        }

        public string Content => Value;

        /// <summary>
        /// 获取或设置 <see cref="IsHighlightInformationChanged"/> 属性。
        /// </summary>
        public int IsHighlightInformationChanged
        {
            get => _isHighlightInformationChanged;
            set => SetValue(ref _isHighlightInformationChanged, value);
        }




        public ObservableCollection<IHighlightToken> Highlights { get; }
    }

}